package fr.pud.client.view;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;

import fr.pud.client.model.Query;
import fr.pud.client.model.Vehicle;
import fr.pud.client.view.mapstation.MapStation;

/**
 * Apr 11, 2012 - Projet_Ose.
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes
 *         MapQuery.java
 *         A MapQuery is the representation of a query ( two PUDStations, one
 *         deliver and one pickUp). Each PUDStation is represented by a
 *         MapStation and a line between them is drawn to link them.
 */
public class MapQuery {
    final float        dash1[] = {
                                   10.0f
                               };
    final BasicStroke  dashed  = new BasicStroke(MapRoute.THICKNESS_LINES,
                                       BasicStroke.CAP_BUTT,
                                       BasicStroke.JOIN_MITER, 10.0f,
                                       this.dash1, 0.0f);
    private MapStation pickup;
    private MapStation delivery;
    private Vehicle    defaultVehicle;

    /**
     * The constructor of a MapQuery
     */
    public MapQuery(Query query) {
        this.defaultVehicle = new Vehicle();
        this.pickup = new MapStation(query.getPickup(), this.defaultVehicle,
                MapStation.ORANGE_POINTER);
        this.delivery = new MapStation(query.getDelivery(),
                this.defaultVehicle, MapStation.ORANGE_POINTER);
    }

    /**
     * @return the defaultVehicle
     */
    public Vehicle getDefaultVehicle() {
        return this.defaultVehicle;
    }

    /**
     * @return the delivery
     */
    public MapStation getDelivery() {
        return this.delivery;
    }

    /**
     * @return the pickup
     */
    public MapStation getPickup() {
        return this.pickup;
    }

    /**
     * draw the Route on the map
     * @param g
     *            , the graphics where the Route is painted
     * @param RoutePoint
     *            , the list of MapPoint of the Route
     */
    public void paint(Graphics g, MapStation pickUp, MapStation delivery,
            Color c) {
        if (pickUp != null && delivery != null) {
            Graphics2D g2 = (Graphics2D) g;
            g2.setColor(c);
            Stroke oldStroke = g2.getStroke();
            pickUp.paintQueriesMarkers(g2, JMapViewerExtended.getJMapViewer()
                    .getMapPosition(pickUp.getCoordonneesAddress()));
            delivery.paintQueriesMarkers(g, JMapViewerExtended.getJMapViewer()
                    .getMapPosition(delivery.getCoordonneesAddress()));
            if (MapQueries.getInstance().isMapQueriesVisible()
                    && JMapViewerExtended.getJMapViewer().getMapPosition(
                            pickUp.getCoordonneesAddress()) != null
                    && JMapViewerExtended.getJMapViewer().getMapPosition(
                            delivery.getCoordonneesAddress()) != null) {
                g2.setStroke(this.dashed);
                g2.drawLine(
                        JMapViewerExtended.getJMapViewer().getMapPosition(
                                pickUp.getCoordonneesAddress()).x,
                        JMapViewerExtended.getJMapViewer().getMapPosition(
                                pickUp.getCoordonneesAddress()).y,
                        JMapViewerExtended.getJMapViewer().getMapPosition(
                                delivery.getCoordonneesAddress()).x,
                        JMapViewerExtended.getJMapViewer().getMapPosition(
                                delivery.getCoordonneesAddress()).y);
                g2.setStroke(oldStroke);
            }
        }
    }

    /**
     * @param aDefaultVehicle
     *            the defaultVehicle to set
     */
    public void setDefaultVehicle(Vehicle aDefaultVehicle) {
        this.defaultVehicle = aDefaultVehicle;
    }

    /**
     * @param aDelivery
     *            the delivery to set
     */
    public void setDelivery(MapStation aDelivery) {
        this.delivery = aDelivery;
    }

    /**
     * @param aPickup
     *            the pickup to set
     */
    public void setPickup(MapStation aPickup) {
        this.pickup = aPickup;
    }
}
